import 'package:bruno/bruno.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import '../../commponets/DropDownDevicePicker.dart';
import '../../home/AppLocale.dart';
import '../../routers/fluro_navigator.dart';
import '../../tools/widgets/my_app_bar.dart';
import '../../tools/widgets/my_button.dart';

class AddHandPage extends StatefulWidget {
  const AddHandPage({
    super.key,
    this.deviceType,
  });

  final int? deviceType;

  @override
  State<AddHandPage> createState() => _AddHandPageState();
}

class _AddHandPageState extends State<AddHandPage> {
  late int _deviceType;
  int? network = 1; // 默认值为1
  bool isAgreed = false;

  @override
  void initState() {
    super.initState();
    _deviceType = widget.deviceType!;
    // _fetchData();
  }

  @override
  Widget build(BuildContext context) {
    // 根据设备类型动态生成标题
    String getTitleByDeviceType() {
      switch (_deviceType) {
        case 1:
          return AppLocale.text('d_sdzhinnegshexinagji');
        case 2:
          return AppLocale.text('d_sdzhinengmenling');
        case 3:
          return AppLocale.text('d_sdzhinengmenjinji');
        case 4:
          return AppLocale.text('d_sdzhinengshineiji');
        default:
          return AppLocale.text('d_sdshebei');
      }
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: MyAppBar(
        centerTitle: getTitleByDeviceType(),
        backgroundColor: Colors.transparent,
        dropdownWidth: 140,
        dropdownBackgroundColor: Colors.white,
        dropdownBorderRadius: 20,
      ),

      body: Column(
        children: [
          const SizedBox(height: 10),

          // 设备类型选择行
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              DropDownDevicePicker(
                title: AppLocale.text('d_youxianpeiwang'),
                titleStyle: const TextStyle(
                  fontSize: 14.0,
                  color: Colors.black,
                ),
                arrowIcon: const Icon(
                  Icons.arrow_drop_down,
                  size: 20,
                  color: Colors.black87,
                ),
                deviceList: [
                  AppLocale.text('d_youxianpeiwang'),
                  AppLocale.text('d_wifipeiwang'),
                ],
                itemStyle: const TextStyle(
                  fontSize: 14.0,
                  color: Color(0xFF595959),
                  fontWeight: FontWeight.bold,
                ),
                dropdownBackgroundColor: Colors.white,
                mainContainerColor: Colors.transparent,
                borderColor: Colors.transparent,
                checkIcon: const Icon(
                  Icons.check,
                  color: Colors.black38,
                ),
                width: 140,
                height: 50,
                alignment: DropDownMainAxisAlignment.spaceEvenly,
                dropdownWidth: 130,
                maxDropdownHeight: 245,
                trianglePosition: 85,
                itemPadding: EdgeInsets.only(left: 2.0),
                onSelected: (device) {
                  print("设备类型选中了: $device");
                  setState(() {
                    switch (device) {
                      case '有线配网':
                        network = 1;
                        break;
                      case 'WiFi配网':
                        network = 2;
                        break;
                    }
                  });
                  // _fetchData();
                },
              ),
            ],
          ),

          // 根据network值动态显示内容
          network == 1
              ? buildWiredNetworkContent() // 有线配网内容(xx1)
              : buildWiFiNetworkContent(), // WiFi配网内容(xx2)

          const SizedBox(height: 20),

          Padding(
            padding: const EdgeInsets.only(left: 40.0, right: 40.0, bottom: 2.0),
            child: MyButton(
              radius: 10,
              minWidth: double.infinity,
              text: AppLocale.text('d_xiayibu'),
              onPressed: () {
                // 下一步逻辑
              },
            ),
          ),



        ],
      ),
    );
  }

  // 有线配网内容(xx1)
  Widget buildWiredNetworkContent() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start, // 子组件靠左排列
          children: [
            Padding(
              padding: EdgeInsets.only(left: 40), // 左侧边距10
              child: Text(
                AppLocale.text('d_chongzhishebei'),
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
        const SizedBox(height: 10),
        Image.asset(
          "images/images/add/r2.png",
        ),
        const SizedBox(height: 10),
        RichText(
          text: TextSpan(
            style: const TextStyle(
              color: Colors.black,
              fontSize: 14,
              height: 1.4,
            ),
            children: [
              TextSpan(text: AppLocale.text('d_chongzhishebeidata')),
            ],
          ),
        ),
        const SizedBox(height: 200),
        // 指示灯部分
        Center(
          child: Container(
            width: double.infinity, // 占满父容器宽度
            padding: const EdgeInsets.symmetric(horizontal: 40), // 添加左右内边距
            child: Wrap(
              alignment: WrapAlignment.center, // 水平居中对齐
              crossAxisAlignment: WrapCrossAlignment.center, // 垂直居中对齐
              spacing: 5, // 子组件之间的水平间距
              children: [
                GestureDetector(
                  onTap: () {
                    setState(() {
                      isAgreed = !isAgreed;
                    });
                  },
                  child: Container(
                    width: 16,
                    height: 16,
                    decoration: BoxDecoration(
                      border: Border.all(color: const Color(0xFFCCCCCC), width: 1),
                      borderRadius: BorderRadius.circular(10),
                      color: isAgreed ? const Color(0xFF007AFF) : Colors.white,
                    ),
                    child: isAgreed
                        ? const Icon(
                      Icons.check,
                      size: 12,
                      color: Colors.white,
                    )
                        : null,
                  ),
                ),
                const SizedBox(width: 2),
                Text.rich(
                  TextSpan(
                    style: const TextStyle(
                      color: Color(0x80333333),
                      fontSize: 12,
                      height: 1.4,
                    ),
                    children: [
                      TextSpan(text: AppLocale.text('d_jietongdianyuandata')),
                    ],
                  ),
                  textAlign: TextAlign.center, // 文本居中对齐
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  // WiFi配网内容(xx2)
  Widget buildWiFiNetworkContent() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start, // 子组件靠左排列
          children: [
            Padding(
              padding: EdgeInsets.only(left: 40), // 左侧边距10
              child: Text(
                AppLocale.text('d_chongzhishebei'),
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
        const SizedBox(height: 10),
        Center(
          child: Container(
            width: double.infinity, // 占满父容器宽度
            padding: const EdgeInsets.symmetric(horizontal: 40),
            child: RichText(
              text: TextSpan(
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  height: 1.4,
                ),
                children: [
                  TextSpan(text: AppLocale.text('d_wifidata')),
                  TextSpan(
                    text: AppLocale.text('d_luyoufangfa'),
                    style: const TextStyle(color: Color(0xFF007AFF)),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        // NavigatorUtils.push(context, '/user-agreement');
                      },
                  ),
                ],
              ),
            ),
          ),
        ),
        Image.asset(
          "images/images/add/r4.png",
        ),
        const SizedBox(height: 230),
      ],
    );
  }



}